home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / RTCLI.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-14  |  4.7 KB  |  201 lines

  1. #include "global.h"
  2. #include "commands.h"
  3. #include "mbuf.h"
  4. #include "netuser.h"
  5. #include "md5.h"
  6. #include "files.h"
  7.  
  8. #if !defined(_lint)
  9. static char rcsid[] OPTIONAL = "$Id: rtcli.c,v 1.1 1997/09/14 14:37:46 root Exp root $";
  10. #endif
  11.  
  12. #ifdef ENCAP
  13.  
  14. static int Slock = -1;
  15. static int routesend (int s, char *challenge);
  16.  
  17. #define RetryInterval        60
  18. #define KeepAliveInterval    60
  19.  
  20.  
  21. int
  22. routecli0 (int argc OPTIONAL, char *argv[] OPTIONAL, void *p OPTIONAL)
  23. {
  24.     Slock = -1;
  25.     return 0;
  26. }
  27.  
  28.  
  29.  
  30. int
  31. routecli1 (int argc, char *argv[], void *p OPTIONAL)
  32. {
  33. struct mbuf *bp;
  34. char challenge[80], *cp;
  35. int done;
  36. int s;
  37. struct sockaddr_in fsocket;
  38.  
  39.     /* handle the administrivia on sockets */
  40.     server_disconnect_io ();
  41.  
  42.     if (Slock != -1)    {
  43.         /* output error message and exit */
  44.         tcmdprintf ("Route client already active! Aborting...\n");
  45.         return 1;
  46.     }
  47.  
  48.     /* initialize the address/port for the connection from user parameters */
  49.     fsocket.sin_family = AF_INET;
  50.     if (argc < 3)
  51.         fsocket.sin_port = IPPORT_ROUTE;
  52.     else
  53.         fsocket.sin_port = (int16) atoip (argv[2]);
  54.  
  55.     if ((fsocket.sin_addr.s_addr = resolve (argv[1])) == 0) {
  56.         tcmdprintf (Badhost, argv[1]);
  57.         return 1;
  58.     }
  59.  
  60.     /* let everyone know we're here! */
  61.     tcmdprintf ("Starting route client to %s on port %d\n", argv[1], fsocket.sin_port);
  62.     log(-1, "open Route Client");
  63.  
  64.     do    {
  65.         done = 0;
  66.  
  67.         /* open a new socket */
  68.         if ((s = socket (AF_INET, SOCK_STREAM, 0)) == -1) {
  69.             tcmdprintf (Nosock);
  70.             return 1;
  71.         }
  72.  
  73.         /* open a connection to the remote site */
  74.         if (connect (s, (char *) &fsocket, SOCKSIZE) == -1)    {
  75.             tcmdprintf ("Route client connection to %s failed! Retry in %d seconds\n", argv[1], RetryInterval);
  76.             log (-1, "Route client connection to %s failed! Retry in %d seconds\n", argv[1], RetryInterval);
  77.             done = 1;
  78.         }
  79.         Slock = s;
  80.         log(-1, "Route Client Connected: %s", argv[1]);
  81.  
  82.         /* read header string */
  83.         if (!done && (recv_mbuf (s, &bp, 0, NULLCHAR, 0) == -1 || !bp || !bp->data))
  84.             done = 1;
  85.  
  86.         if (!done && bp && bp->data)    {
  87.             cp = strrchr ((char *)bp->data, '<');
  88.             if (cp == NULLCHAR)    {
  89.                 done = 1;
  90.                 /* log and abort */
  91.                 log (s, "No challenge string received from remote\n");
  92.                 done = 1;
  93.             } else    {
  94.                 rip (cp);
  95.                 strcpy (challenge, cp);
  96.     
  97.                 if (routesend (s, challenge) <= 0)    {
  98.                     /* none sent, exit */
  99.                     log (-1, "No routes to send to remote - aborting...\n");
  100.                     done = 1;
  101.                     Slock = -1;
  102.                 }
  103.             }
  104.         }
  105.     
  106.         /* stay up until killed */
  107.         while (!done && Slock != -1)    {
  108.             kpause (KeepAliveInterval * 1000L);
  109.             /* also check for disconnections from the server.
  110.                send 'NOOP' commands, and check for "+OK" response.
  111.              */
  112.             usprintf (s, "NOOP\n");
  113.             if (recv_mbuf (s, &bp, 0, NULLCHAR, 0) == -1 || !bp || !bp->data)
  114.                 done = 1;
  115.             else    {
  116.                 if (strncmp ((char *)bp->data, "+OK ", 4))
  117.                     done = 1;
  118.             }
  119.         }
  120.         close_s (s);
  121.         log(-1, "Route Client Disconnected: %s", argv[1]);
  122.         if (Slock != -1)
  123.             kpause (RetryInterval * 1000L);
  124.     } while (Slock != -1);
  125.     
  126.     log(s, "close Route Client");
  127.     return 0;
  128. }
  129.  
  130.  
  131.  
  132. static int
  133. routesend (int s, char *challenge)
  134. {
  135. FILE *fp;
  136. char buf[80], *rt, *auth, *cp;
  137. MD5_CTX mdContext;
  138. char missive[80];
  139. unsigned char *dp, *ep;
  140. int sent = 0;
  141. struct mbuf *bp;
  142.  
  143.     /* this function looks up the given route in the 'etc/encap-rt.dat'
  144.        file. The format of each line in that file is:
  145.  
  146.        routestring        authenticationstring
  147.  
  148.      */
  149.     if ((fp = fopen (EncapRoutes, READ_TEXT)) == NULLFILE)
  150.         return sent;
  151.  
  152.     while (fgets (buf, 80, fp))    {
  153.         rip (buf);
  154.         rt = skipwhite (buf);
  155.         if (!*rt || *rt == '#')
  156.             continue;        /* skip comment lines */
  157.         auth = skipnonwhite (rt);
  158.         if (!*auth)
  159.             continue;
  160.         *auth++ = 0;
  161.  
  162.         auth = skipwhite (auth);
  163.         if (!*auth)
  164.             continue;
  165.  
  166.         /* now look for trailing white space and remove if found */
  167.         cp = skipnonwhite (auth);
  168.         if (*cp)
  169.             *cp = 0;
  170.  
  171.         /* now calculate the MD5 string that we SHOULD have received */
  172.         MD5Init (&mdContext);
  173.         MD5Update (&mdContext, (unsigned char *) challenge, strlen (challenge));
  174.         MD5Update (&mdContext, (unsigned char *) rt, strlen (rt));
  175.         MD5Update (&mdContext, (unsigned char *) auth, strlen (auth));
  176.         MD5Final (&mdContext);
  177.  
  178.         cp = missive;
  179.         dp = mdContext.digest;
  180.         ep = dp + sizeof (mdContext.digest);
  181.         for (; dp < ep; cp += 2)
  182.             (void) sprintf (cp, "%02x", *dp++ & 0xff);
  183.         *cp = '\0';
  184.  
  185.         usprintf (s, "route %s %s\n", rt, missive);
  186.         sent++;
  187.         
  188.         if (recv_mbuf (s, &bp, 0, NULLCHAR, 0) == -1 || !bp || !bp->data)
  189.             break;
  190.         if (!strncmp ("-ERR ", (char *) bp->data, 5))    {
  191.             (void) fclose (fp);
  192.             return -1;
  193.         }
  194.     }
  195.  
  196.     (void) fclose (fp);
  197.     return sent;
  198. }
  199.  
  200. #endif /* ENCAP */
  201.